package com.itheima.employeesys.ui;
import com.itheima.employeesys.bean.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class LoginUI extends JFrame {
    private ArrayList<User> users = new ArrayList<>(){{
            add(new User("admin","123456","admin"));
    }};//所有用户集合
    private JPanel mainPanel;
    private JPanel headerPanel;
    private JPanel formPanel;
    private JPanel buttonPanel;
    private JTextField usernameField; // 添加用户名字段
    private JPasswordField passwordField;// 添加密码字段
    private JButton loginButton;
    private JButton registerButton;
    private JLabel titleLabel;
    private JLabel usernameLabel;
    private JLabel passwordLabel;
//    private JCheckBox rememberMeCheckBox;

    public LoginUI() {
        initializeComponents();
        setupLayout();
        setupEventHandlers();
        setFrameProperties();
    }

    private void initializeComponents() {
        //用户容器

        // 主面板
        mainPanel = new JPanel();

        // 标题面板
        headerPanel = new JPanel();
        titleLabel = new JLabel("人事信息管理系统");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setForeground(new Color(70, 130, 180)); // Steel blue color

        // 表单面板
        formPanel = new JPanel();
        usernameLabel = new JLabel("登录名:");
        usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        usernameField = new JTextField(15);
        usernameField.setPreferredSize(new Dimension(200, 30));
        usernameField.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        passwordLabel = new JLabel("密  码:");
        passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        passwordField = new JPasswordField(15);
        passwordField.setPreferredSize(new Dimension(200, 30));
        passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 14));

//        rememberMeCheckBox = new JCheckBox("记住我");
//        rememberMeCheckBox.setFont(new Font("微软雅黑", Font.PLAIN, 12));
//        rememberMeCheckBox.setFocusPainted(false);

        // 按钮面板
        buttonPanel = new JPanel();
        loginButton = new JButton("登录");
        loginButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        loginButton.setBackground(new Color(70, 130, 180)); // Steel blue
        loginButton.setForeground(Color.WHITE);
        loginButton.setFocusPainted(false);
        loginButton.setBorderPainted(false);
        loginButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        registerButton = new JButton("注册");
        registerButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        registerButton.setBackground(new Color(70, 130, 180)); // Steel blue
        registerButton.setForeground(Color.WHITE);
        registerButton.setFocusPainted(false);
        registerButton.setBorderPainted(false);
        registerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

    }

    private void setupLayout() {
        // 设置主面板布局
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 30, 20, 30));
        mainPanel.setBackground(Color.WHITE);

        // 设置标题面板
        headerPanel.setBackground(Color.WHITE);
        headerPanel.add(titleLabel);

        // 设置表单面板布局
        formPanel.setLayout(new GridBagLayout());
        formPanel.setBackground(Color.WHITE);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.anchor = GridBagConstraints.WEST;

        // 用户名行
        gbc.gridx = 0; gbc.gridy = 0;
        formPanel.add(usernameLabel, gbc);
        gbc.gridx = 1;
        formPanel.add(usernameField, gbc);

        // 密码行
        gbc.gridx = 0; gbc.gridy = 1;
        formPanel.add(passwordLabel, gbc);
        gbc.gridx = 1;
        formPanel.add(passwordField, gbc);

//        // 记住我复选框
//        gbc.gridx = 1; gbc.gridy = 2;
//        gbc.anchor = GridBagConstraints.WEST;
//        formPanel.add(rememberMeCheckBox, gbc);

        // 设置按钮面板
        buttonPanel.setBackground(Color.WHITE);
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
        buttonPanel.add(loginButton);
        buttonPanel.add(registerButton);

        // 将各面板添加到主面板
        mainPanel.add(headerPanel, BorderLayout.NORTH);
        mainPanel.add(formPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);
    }
//登录按钮事件处理--------------------------------------------------------------------------124
    private void setupEventHandlers() {
        // 登录按钮事件
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                performLogin();
            }
        });

        // 注册按钮事件
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 添加注册逻辑

                performRegister();
                //JOptionPane.showMessageDialog(LoginUI.this, "注册功能待实现", "提示", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 回车键登录功能
        usernameField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                passwordField.requestFocus();
            }
        });

        passwordField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                performLogin();
            }
        });
    }

    private void performRegister() {
        RegisterDialog registerDialog = new RegisterDialog(LoginUI.this, users);
        registerDialog.setVisible(true);
    }


    private void performLogin() {
        String loginName = usernameField.getText().trim();
        String password = new String(passwordField.getPassword());

        if (loginName.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入登录名！", "提示", JOptionPane.WARNING_MESSAGE);
            usernameField.requestFocus();
            return;
        }

        if (password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入密码！", "提示", JOptionPane.WARNING_MESSAGE);
            passwordField.requestFocus();
            return;
        }

        // 使用users集合进行验证
        boolean loginSuccess = false;
        String userName = "NULL";
        for (User user : users) {
            if (user.getLoginName().equals(loginName) && user.getPassword().equals(password)) {
                userName = user.getUsername();
                loginSuccess = true;
                break;
            }
        }

        if (loginSuccess) {
            JOptionPane.showMessageDialog(this, "登录成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
            this.dispose(); // 关闭登录窗口
            // 跳转到员工管理系统窗口
            new EmployeeManager(userName);

        } else {
            JOptionPane.showMessageDialog(this, "登录名或密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
            passwordField.setText("");
            passwordField.requestFocus();
        }
    }

    private void setFrameProperties() {
        setTitle("系统登录");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null); // 居中显示
        setResizable(false); // 不允许调整大小
    }

    public static void main(String[] args) {
        // 设置外观为系统默认外观
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LoginUI().setVisible(true);
            }
        });
    }
}